﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.DirectX.DirectSound;

namespace jacDigitalDrum
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private System.IO.Ports.SerialPort sp;

        public Window1()
        {
            InitializeComponent();
            WPF.Themes.ThemeManager.ApplyTheme(Application.Current, "ExpressionDark");
            #region ins
            hasWin();
            s1.tbk_title.Text = n1.tbk_title.Text = "A0";
            s2.tbk_title.Text = n2.tbk_title.Text = "A1";
            s3.tbk_title.Text = n3.tbk_title.Text = "A2";
            s4.tbk_title.Text = n4.tbk_title.Text = "A3";
            s5.tbk_title.Text = n5.tbk_title.Text = "A4";
            s6.tbk_title.Text = n6.tbk_title.Text = "A5";
            Application.Current.Exit += new ExitEventHandler(Current_Exit);
            #endregion

            this.bt_set.Click += new RoutedEventHandler(bt_set_Click);
            this.bt_edit.Click += new RoutedEventHandler(bt_edit_Click);
            this.Loaded += new RoutedEventHandler(Window1_Loaded);
            lb_list.SelectionChanged += new SelectionChangedEventHandler(lb_list_SelectionChanged);
            this.bt_to.Click += new RoutedEventHandler(bt_to_Click);
            this.bt_refresh.Click += new RoutedEventHandler(bt_refresh_Click);
            this.bt_del.Click += new RoutedEventHandler(bt_del_Click);
        }

        void bt_to_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://blog.csdn.net/jacle169");
        }

        void bt_set_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                sp = new System.IO.Ports.SerialPort(cbo_com.SelectionBoxItem.ToString(), 19200);
                sp.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(sp_DataReceived);
                sp.ReceivedBytesThreshold = computContract();
                if (!sp.IsOpen)
                {
                    sp.Open();
                    bt_set.IsEnabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            } 
        }

        int computContract()
        {
            int d = int.Parse(((ComboBoxItem)cbo_model.SelectedItem).Tag.ToString());
            return (d - 1) * 5 + 4;
        }

        void sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            var data = sp.ReadExisting();

            string[] indata = data.Split(',');

            if (int.Parse(indata[0]) > 20)
            {
                ((SecondaryBuffer)sounds[0]).SetCurrentPosition(0);
                ((SecondaryBuffer)sounds[0]).Play(0, BufferPlayFlags.Default);
            }

            if (int.Parse(indata[1]) > 20)
            {
                ((SecondaryBuffer)sounds[1]).SetCurrentPosition(0);
                ((SecondaryBuffer)sounds[1]).Play(0, BufferPlayFlags.Default);
            }

            if (int.Parse(indata[2]) > 20)
            {
                ((SecondaryBuffer)sounds[2]).SetCurrentPosition(0);
                ((SecondaryBuffer)sounds[2]).Play(0, BufferPlayFlags.Default);
            }

            if (int.Parse(indata[3]) > 20)
            {
                ((SecondaryBuffer)sounds[3]).SetCurrentPosition(0);
                ((SecondaryBuffer)sounds[3]).Play(0, BufferPlayFlags.Default);
            }

            if (int.Parse(indata[4]) > 20)
            {
                ((SecondaryBuffer)sounds[4]).SetCurrentPosition(0);
                ((SecondaryBuffer)sounds[4]).Play(0, BufferPlayFlags.Default);
            }

            if (int.Parse(indata[5]) > 20)
            {
                ((SecondaryBuffer)sounds[5]).SetCurrentPosition(0);
                ((SecondaryBuffer)sounds[5]).Play(0, BufferPlayFlags.Default);
            }
        }

        private SecondaryBuffer LoadSoundFile(string name)
        {
            try
            {
                BufferDescription buffDes = new BufferDescription();
                buffDes.GlobalFocus = true;//设置缓冲区全局获取焦点
                buffDes.ControlVolume = true;//指明缓冲区可以控制声音
                buffDes.ControlPan = true;//指明缓冲区可以控制声道平衡
                return new SecondaryBuffer(name, buffDes, ApplicationDevice);
            }
            catch
            {
                return null;
            }
        }
    }
}
